package org.bright.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.bright.entity.PageResult;
import org.bright.entity.Products;
import org.bright.entity.R;
import org.bright.entity.dto.ProductDTO;
import org.bright.entity.vo.ProductVO;
import org.bright.service.IProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bright
 * @since 2024-12-10 04:12
 */
@RestController
@RequestMapping("/products")
@Tag(name = "商品管理", description = "商品接口")
public class ProductsController {
    @Autowired
    private IProductsService productsService;
    @GetMapping
    @Operation(summary = "商品列表接口", description = "获取商品列表")
    public R getList(@RequestBody ProductDTO productDto) {
        PageResult<ProductVO> page = productsService.getPage(productDto);
        return R.success(page);
    }
    @PostMapping
    @Operation(summary = "新增商品接口", description = "新增商品")
    public R add(@RequestBody Products products) {
        boolean save = productsService.save(products);
        if (save) {
            return R.success();
        }
        return R.fail();
    }
    @DeleteMapping("{id}")
    @Operation(summary = "删除商品接口", description = "删除商品")
    public R delete(@PathVariable Integer id) {
        boolean removeById = productsService.removeById(id);
        if (removeById) {
            return R.success();
        }
        return R.fail();
    }
    @GetMapping("{id}")
    @Operation(summary = "获取商品信息接口", description = "通过id获取商品信息")
    public R getById(@PathVariable Integer id) {
        Products products = productsService.getById(id);
        return R.success(products);
    }
    @PutMapping
    @Operation(summary = "修改商品信息接口", description = "修改商品")
    public R update(@RequestBody Products products) {
        boolean updateById = productsService.updateById(products);
        if (updateById) {
            return R.success();
        }
        return R.fail();
    }
}
